package com.woniu.intlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.proscheduingcommand.AddProScheduingCommand;
import com.woniu.command.proscheduingcommand.DeleteProScheduingCommand;
import com.woniu.command.proscheduingcommand.UpdateProScheduingCommand;
import com.woniu.command.scheworkcommand.AddScheWorkCommand;
import com.woniu.command.scheworkcommand.DeleteScheWorkCommand;
import com.woniu.intlet.web.jsr303.PurchasePoJsr;
import com.woniu.outlet.po.ProSchedulingPo;
import com.woniu.service.ProSchedulingPoService;
import com.woniu.uitl.ResponseResult;
import com.woniu.uitl.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "生产排程接口文档")
public class ProSchedulingController {

    @Autowired
    private ProSchedulingPoService proSchedulingPoService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("findAllProSche")
    @ApiOperation(value = "生产排程列表")
    @ApiOperationSupport(ignoreParameters = {"endTime","orderNum",
            "proName","proNum",
            "proPlanId","proSpecifications",
            "processName","spare1","spare2","spare3","startTime","submissionTime","workCodes","workerId","spare1"
            ,"workerName","workingNumrs","workingProcedureCode"})
    public ResponseResult<List<ProSchedulingPo>> findAllProSche(ProSchedulingPo po){
        List<ProSchedulingPo> proShces = proSchedulingPoService.findAllProShces(po);
        return new ResponseResult<List<ProSchedulingPo>>(proShces,"OK",ResultCode.SUCCESS.getCode());
    }

    @GetMapping("getProSche")
    @ApiOperation(value = "根据id查询生产排程")
    @ApiOperationSupport(ignoreParameters = {"spare3","spare2",
            "submissionTime","workingProcedureCode",
            "processName","proNum",
            "workCodes","workerId","submissionTime"})
    public ResponseResult<ProSchedulingPo> getProPlanById(Long id){
        ProSchedulingPo proScheById = proSchedulingPoService.getProScheById(id);
        return new ResponseResult<ProSchedulingPo>(proScheById,"OK",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("addProSche")
    @ApiOperation(value = "添加生产排程")
    @ApiOperationSupport(ignoreParameters = {"workingProcedureCode","processName",
            "proNum","workingNumrs",
            "spare2","spare3",
            "status","id","workerName","submissionTime"})
    public ResponseResult<String> aaddProSche(@RequestBody ProSchedulingPo po){
        AddProScheduingCommand command = new AddProScheduingCommand();
        String msg = command.excute(po);
        return new ResponseResult<>(ResultCode.SUCCESS.getCode(),msg);
    }

    @DeleteMapping("deleteProSche")
    @ApiOperation(value = "删除生产排程")
    public ResponseResult<String> deleteProSche(Long id){
        DeleteProScheduingCommand command = new DeleteProScheduingCommand();
        String msg = command.excute(id);
        return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
    }

    @PutMapping("updateProSche")
    @ApiOperation(value = "修改未提交的生产排程")
    @ApiOperationSupport(ignoreParameters = {"workingProcedureCode","processName",
            "proNum","workingNumrs",
            "spare2","spare3",
            "spare1","status","workCodes","submissionTime","workerName"})
    public ResponseResult<Map> updateProSche(@Validated(value = {PurchasePoJsr.class}) ProSchedulingPo po, BindingResult result){
        HashMap<String, String> map = new HashMap<>();
        if (result.hasErrors()){
            result.getFieldErrors().forEach(item ->{
                map.put(item.getField(),item.getDefaultMessage());
            });
            return new ResponseResult<Map>(ResultCode.PARAM_NOT_VALID.getCode(),"参数不合法",map);
        }
        UpdateProScheduingCommand command = new UpdateProScheduingCommand();
        String msg = command.excute(po);
        map.put("OK",msg);
        return new ResponseResult<Map>(ResultCode.SUCCESS.getCode(),"OK",map);
    }


    @PostMapping("addWorking")
    @ApiOperation("给生产排程添加工序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "proScheId",required = true,value = "生产排程id"),
            @ApiImplicitParam(name = "workProCode",required = true,value = "工序编码")
    })
    public ResponseResult<String> addWorking(Long proScheId,String workProCode){
        AddScheWorkCommand command = new AddScheWorkCommand();
        String msg = command.excute(proScheId, workProCode);
        return new ResponseResult<>(ResultCode.SUCCESS.getCode(),msg);
    }

    @DeleteMapping("delete")
    @ApiOperation("给生产排程删除工序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "proScheId",required = true,value = "生产排程id"),
            @ApiImplicitParam(name = "workProCode",required = true,value = "工序编码")
    })
    public ResponseResult<String> deleteWorking(Long proScheId,String workProCode){
        DeleteScheWorkCommand workCommand = new DeleteScheWorkCommand();
        String msg = workCommand.excute(proScheId, workProCode);
        return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
    }

    @PutMapping("updateProScheStatus")
    @ApiOperation("提交生产排程")
    public ResponseResult<String> updateStatys(@RequestHeader(name = "redisToken") String redisToken,Long proScheId){
        if (redisTemplate.delete(redisToken)){
            UpdateProScheduingCommand command = new UpdateProScheduingCommand();
            String status = "已提交";
            String msg = command.excute(proScheId,status);
            return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
        }
        return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),"请不要频繁提交");
    }


}
